package com.yorick.login.service.impl;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yorick.login.entity.Account;
import com.yorick.login.protobuf.Login;
import com.yorick.netty.WebSocketServerHandler;
import com.yorick.netty.messageListener.LogicService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service("0")
public class WebSocketLoginServiceImpl extends WebSocketServerHandler implements LogicService {

    @Override
    public void task(byte[] data, ChannelHandlerContext ctx) throws InvalidProtocolBufferException {
        Login.C2SLogin login = Login.C2SLogin.parseFrom(data);
        ctx.channel().attr(AttributeKey.valueOf("account")).set(login.getAccount());
        Account.channels.put(login.getAccount(), ctx.channel());
        log.info("{}登录进来了", login.getAccount());
    }
}
